import { changePayPass, changeLoginPass } from "@/api/user.js";
export default {
  data() {
    return {};
  },
  methods: {
    // type=1 交易密码   2  登陆密码
    submit(model, type) {
      console.log(type);
      if (!model.oldPass) {
        this.$util.Tips({
          title: "原密码不能为空",
        });
        return;
      }
      if (this.validatePassword(model.newPass, type)) {
        if (model.newPass != model.verifyNewPass) {
          this.$util.Tips({
            title: "确认密码与新密码输入不一致",
          });
          return;
        }
        if (type == 1) {
          changePayPass
            .then((res) => {
              this.$util.Tips({
                title: res.message,
              });
              setTimeout(() => {
                uni.navigateBack();
              }, 1000);
            })
            .catch((err) => {
              this.$util.Tips({
                title: err,
              });
            });
        } else {
          changeLoginPass(model)
            .then((res) => {
              this.$util.Tips({
                title: res.message,
              });
              setTimeout(() => {
                uni.navigateBack();
              }, 1000);
            })
            .catch((err) => {
              this.$util.Tips({
                title: err,
              });
            });
        }
      }
    },
    validatePassword(password, type) {
      const minLength = type == 1 ? 6 : 8;
      const maxLength = 12;
      const hasUpperCase = /[A-Z]/.test(password);
      const hasLowerCase = /[a-z]/.test(password);
      const hasNumbers = /\d/.test(password);
      if (!password) {
        this.$util.Tips({
          title: "新密码输入不能为空",
        });
        return false;
      }

      if (type == 2) {
        if (password.length < minLength || password.length > maxLength) {
          this.$util.Tips({
            title:
              "密码长度应大于" + minLength + "位，并且小于" + maxLength + "位",
          });
          return false;
        }

        if (!hasUpperCase) {
          this.$util.Tips({
            title: "密码必须包含至少一个大写字母",
          });
          return false;
        } else if (!hasLowerCase) {
          this.$util.Tips({
            title: "密码必须包含至少一个小写字母",
          });
          return false;
        } else if (!hasNumbers) {
          this.$util.Tips({
            title: "密码必须包含至少一个数字",
          });
          return false;
        }
      } else {
        if (password.length < minLength) {
          this.$util.Tips({
            title: "密码长度应等于" + minLength + "位",
          });
          return false;
        }
      }
      return true;
    },
  },
};
